package com.brkj.course;

import java.util.List;

import net.tsz.afinal.FinalBitmap;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.brkj.model.DS_Course;
import com.brkj.model.DS_CourseClass;
import com.brkj.main.R;
import com.brkj.util.ImgShow;
import com.nostra13.universalimageloader.core.ImageLoader;

public class CourseClassAdapter extends BaseAdapter{
	private ListItemView listItemView;
	protected LayoutInflater listContainer; 
	private Context context;
	private List<DS_CourseClass> courseClassList;
	private String courseType;
	
	public CourseClassAdapter(Context context, List<DS_CourseClass> courseClassList, String courseType){
		this.context = context;
		this.courseClassList = courseClassList;
		this.courseType = courseType;
		
		listContainer = LayoutInflater.from(context);
	}
	
	private static class ListItemView{
		public TextView courseClassName;
	};
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return courseClassList.size() + 1;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// 自定义视图
		listItemView = new ListItemView();
		// 获取list_item布局文件的视图
		if (convertView == null) {
			convertView = listContainer.inflate(R.layout.course_class_item, null);
			// 获取控件对象
			listItemView.courseClassName = (TextView) convertView.findViewById(R.id.courseClassName);

			// 设置控件集到convertView
			convertView.setTag(listItemView);
		} else {
			listItemView = (ListItemView) convertView.getTag();
		}
		
		if(position == 0){
			listItemView.courseClassName.setText("全部课程");
		}else{
			listItemView.courseClassName.setText(courseClassList.get(position -1).getCourseClassName());
		}
		
		int selecPos; 
		if(courseType.equals(((CourseActivity)context).COURSE_TYPE_PUBLIC)){
			selecPos = ((CourseActivity)context).selectedClass_public;
		}else{
			selecPos = ((CourseActivity)context).selectedClass_post;
		}
		
		if(selecPos == position){
			listItemView.courseClassName.setBackgroundResource(R.drawable.kechenganniu_03);
		}else{
			listItemView.courseClassName.setBackgroundResource(R.drawable.none);
		}
		
		listItemView.courseClassName.setOnClickListener(new myOnclickListener(position));
		
		return convertView;
	}
	
	class myOnclickListener implements OnClickListener{
		private int position;
		
		public myOnclickListener(int position){
			this.position = position;
		}

		
		@Override
		public void onClick(View v) {
			if(courseType.equals(((CourseActivity)context).COURSE_TYPE_PUBLIC)){
				((CourseActivity)context).selectedClass_public = position;
				((CourseActivity)context).selectedClass_post = -1;
			}else{
				((CourseActivity)context).selectedClass_public = -1;
				((CourseActivity)context).selectedClass_post = position;
			}
			
			notifyDataSetInvalidated();
			
			if(position == 0){
				((CourseActivity)context).showCourse(courseType, 0, 0);
			}else{
				((CourseActivity)context).showCourse(courseType, courseClassList.get(position-1).getCourseClassID(), 0);
			}
			((CourseActivity)context).hidePopWindow();
			
		}
		
	}
}
